package com.banggood.upms.web;

import com.banggood.silk.web.api.common.PagedResult;
import com.banggood.upms.bean.po.Permission;
import com.banggood.upms.bean.vo.UserPermissionVo;
import com.banggood.upms.service.UserTreeService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author XuChuangFeng
 */
@RequestMapping("/user-perm")
@RestController
@Api(value = "用户与权限管理", description = "提供查看用户已分配权限的接口。")
public class UserPermController {

    protected final Log logger = LogFactory.getLog(getClass());
    @Autowired
    private UserTreeService userTreeService;

    /**
     * 根据用户ID以及系统标识（系统code）
     *
     * @param vo
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @ApiOperation(value = "用户所有权限的检索", notes = "userid跟accountName必须传入一个，不递归查找。", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户的主键", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "accountName", value = "用户账号", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "systemCode", value = "系统码", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "systemId", value = "系统主键", required = false, paramType = "query", dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 1008, message = "用户权限搜索参数异常!")
    })
    public PagedResult<Permission> selectUserPerm(UserPermissionVo vo) throws Exception {
        List<Permission> userPerms = userTreeService.selectUserPerms(vo);
        long total = new PageInfo<>(userPerms).getTotal();
        PagedResult<Permission> result = new PagedResult<>(userPerms, total);
        return result;
    }
}
